#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#include "stocks.h"
#include "yield_curve.h"

using namespace std;

Stocks::Stocks()
{
	stock_price = 59.72;
	forward_price = 0;
	maturity = 4.5;
}

Stocks::Stocks(double stockPr, double mat)
{
	stock_price = stockPr;
	forward_price = 0;
	maturity = mat;
}

void Stocks::readData(char* input_file)
{
	FILE* fp = fopen(input_file, "r");

	double dividend;

	while (fscanf(fp, "%lf", &dividend) != EOF)
		dividends.push_back(dividend);
	
	fclose(fp);
}

double Stocks::getForwardPrice()
{
	double beginning_t = 0.25;
	double sum = 0;

	YieldCurve yc;
	yc.ReadSwapFile("input.txt");

	for (int i = 0; i < dividends.size(); i++) {
		dates.push_back(beginning_t);
		beginning_t += 0.25;
	}

	for (int i = 0; i < dates.size(); i++)
	{	sum += dividends[i]*yc.DiscountFactor(dates[i]);
//		cout << sum << endl;
	}
	cout << "spotrate is: " << yc.SpotRate(maturity) << endl;

	cout << "sum is: " << sum << endl;

	forward_price = (stock_price - sum)*(1+yc.SpotRate(maturity));

	return forward_price;
}

void Stocks::printDebug()
{
	/*
	for (int i = 0; i < dates.size(); i++)
		cout << "dates[" << i << "] = " << dates[i] << endl;
	*/

	cout << "Stock price is " << stock_price << endl;
	cout << "the formula is (stock_price-sum)*spotrate(maturity)" << endl;
	cout << "Forward Price is " << forward_price << endl;
}
